home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / FTP / Mirror2.3 / dateconv.pl < prev    next >
Encoding:
Perl Script  |  1994-01-28  |  3.3 KB  |  128 lines

  1. # Convert a date into a time.
  2. # By Lee McLoughlin <lmjm@doc.ic.ac.uk>
  3. #  You can do what you like with this except claim that you wrote it or
  4. #  give copies with changes not approved by Lee.  Neither Lee nor any other
  5. #  organisation can be held liable for any problems caused by the use or
  6. #  storage of this package.
  7. # $Id: dateconv.pl,v 2.3 1994/01/28 17:58:21 lmjm Exp lmjm $
  8. # $Log: dateconv.pl,v $
  9. # Revision 2.3  1994/01/28  17:58:21  lmjm
  10. # Added parsing of CTAN (tex archive) dates an the two common HTTP dates.
  11. #
  12. # Revision 2.2  1993/12/14  11:09:05  lmjm
  13. # Correct order of packages.
  14. # Make sure use_timelocal defined.
  15. #
  16. # Revision 2.1  1993/06/28  15:04:22  lmjm
  17. # Full 2.1 release
  18. #
  19.  
  20. # input date and time string from ftp "ls -l" format ("Feb 01 13:25"),
  21. # return data and time string in Unix format "dd Mmm YY HH:MM", "such as
  22. # "1 Feb 92 13:25"
  23. sub lstime_to_standard
  24. {
  25.     local( $ls ) = @_;
  26.  
  27.     return &time_to_standard( &lstime_to_time( $ls ) );
  28. }
  29.  
  30.  
  31. require 'timelocal.pl';
  32. package dateconv;
  33.  
  34. # Use timelocal rather than gmtime.
  35. $use_timelocal = 1;
  36.  
  37. @months = ( "zero", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" );
  38.  
  39. $month_num{ "jan" } = 0;
  40. $month_num{ "feb" } = 1;
  41. $month_num{ "mar" } = 2;
  42. $month_num{ "apr" } = 3;
  43. $month_num{ "may" } = 4;
  44. $month_num{ "jun" } = 5;
  45. $month_num{ "jul" } = 6;
  46. $month_num{ "aug" } = 7;
  47. $month_num{ "sep" } = 8;
  48. $month_num{ "oct" } = 9;
  49. $month_num{ "nov" } = 10;
  50. $month_num{ "dec" } = 11;
  51.  
  52. ( $mn, $yr ) = (localtime)[ 4, 5 ];
  53.  
  54.  
  55. # input date and time string from ftp "ls -l", such as Mmm dd yyyy or
  56. # Mmm dd HH:MM,
  57. # return $time number via gmlocal( $string ).
  58. sub main'lstime_to_time
  59. {
  60.     package dateconv;
  61.  
  62.     local( $date ) = @_;
  63.  
  64.     local( $mon, $day, $hours, $mins, $month, $year );
  65.     local( $secs ) = 0;
  66.  
  67.     # Unix ls, dls and Netware
  68.     if( $date =~ /^(\w\w\w)\s+(\d+)\s+((\d\d\d\d)|((\d+):(\d+)))$/ ){
  69.         ($mon, $day, $year, $hours, $mins) = ($1, $2, $4, $6, $7);
  70.     }
  71.     elsif( $date =~ /^(\d+)\s+(\w\w\w)\s+((\d\d\d\d)|((\d+):(\d+)))$/ ){
  72.         ($day, $mon, $year, $hours, $mins) = ($1, $2, $4, $6, $7);
  73.     }
  74.     # VMS style
  75.     elsif( $date =~ /(\d+)-(\S+)-(\d+)\s+(\d+):(\d+)/ ){
  76.         ($day, $mon, $year, $hours, $mins) = ($1, $2, $3, $4, $5);
  77.     }
  78.     # CTAN style (and HTTP)
  79.     elsif( $date =~ /^\w+\s+(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)\s+(\d+)/ ){
  80.         ($mon, $day, $hours, $mins, $secs, $year ) =
  81.             ($1, $2, $3, $4, $5, $6);
  82.     }
  83.     # another HTTP
  84.         elsif( $date =~ /^\w+,\s+(\d+)-(\w+)-(\d+)\s+(\d+):(\d+):(\d+)/ ){
  85.                 ($day, $mon, $year, $hours, $mins, $secs ) =
  86.                         ($1, $2, $3, $4, $5, $6);
  87.         }
  88.     else {
  89.         printf STDERR "invalid date $date\n";
  90.         return time;
  91.     }
  92.     
  93.     if( $mon =~ /^\d+$/ ){
  94.         $month = $mon - 1;
  95.     }
  96.     else {
  97.         $mon =~ tr/A-Z/a-z/;
  98.         $month = $month_num{ $mon };
  99.     }
  100.  
  101.     if( $year !~ /\d\d\d\d/ ){
  102.         $year = $yr;
  103.         $year-- if( $month > $mn );
  104.     }
  105.     if( $year > 1900 ){
  106.         $year -= 1900;
  107.     }
  108.      
  109.     if( $use_timelocal ){
  110.         return &'timelocal( $secs, $mins, $hours, $day, $month, $year );
  111.     }
  112.     else {
  113.         return &'timegm( $secs, $mins, $hours, $day, $month, $year );
  114.     }
  115. }
  116.  
  117. # input time number, output GMT string as "dd Mmm YY HH:MM"
  118. sub main'time_to_standard
  119. {
  120.     package dateconv;
  121.  
  122.     local( $time ) = @_;
  123.  
  124.     local( $sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst ) =
  125.          gmtime( $time );
  126.     return sprintf( "%2d $months[ $mon + 1 ] %2d %02d:%02d", $mday, $year, $hour, $min );
  127. }
  128.